package com.jpa.redis;

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.alibaba.dubbo.config.annotation.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisClient {

	private JedisPool jedisPool;

	/**
	 * setVExpire(设置key值，同时设置失效时间 秒
	 */
	public void set(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	public void set(String key, Object value) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key.getBytes(), serialize(value));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	/**
	 * (存入redis数据)
	 */
	public void expire(String key, String value, Integer times) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key, value);
			jedis.expire(key, times);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	public void expire(String key, Object value, Integer times) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.set(key.getBytes(), serialize(value));
			jedis.expire(key.getBytes(), times);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	/**
	 * 删除redis数据
	 */
	public void del(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.del(key);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
	}

	public String get(String key) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			String s = jedis.get(key);
			return s;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.close(jedis);
		}
		return null;
	}

	public Object getObj(String key) {
		if (key != null) {
			Jedis jedis = null;
			try {
				jedis = jedisPool.getResource();
				byte[] s = jedis.get(key.getBytes());
				return unserialize(s);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				this.close(jedis);
			}
		}
		return null;
	}

	/**
	 * 释放连接
	 * 
	 * @param jedis
	 */
	public void close(Jedis jedis) {
		if (jedis != null) {
			jedis.close();
			if (jedis.isConnected()) {
				try {
					jedis.disconnect();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}

	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {

		}
		return null;
	}

	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {

		}
		return null;
	}
}
